/* $RCSFile: FlashModule.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/FlashModule/FlashModule.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:20  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:54  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Helpers;
using AddonNice.Settings;
using AddonNice.Diagnostics;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.UI.DataTypes;


using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.FlashModule
{
	public class flStr
	{
		public const string FlashSrc			=   "FLASH_SRC";
		public const string FlashWidth		    =   "FLASH_WIDTH";
		public const string FlashHeight		    =   "FLASH_HEIGHT";
		public const string FlashBackColor	    =   "FLASH_BACKCOLOR";
		public const string FlashPath		    =   "FLASH_PATH";
        public const string SHOW_FLASH_GALLERY  =   "SHOW_FLASH_GALLERY";
	}

	
    /// <summary>
    /// Initializer class for FlashModule 
    /// </summary>
    public class FlashModuleSI : PortalModuleControlSI
    {
        public FlashModuleSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FlashModuleSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FlashModuleSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FlashModuleSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                   =   pm.ExtendedResourcesGroup;
			
            SettingItemSpecial src			=	new SettingItemSpecial(flStr.FlashSrc,new StringDataType ());
			src.Required			        =	true;
			src.Value				        =	"effect2-marquee.swf";
			src.ExtendedGroupName           =   extgrp;
			src.Order				        =	20; //1;
			bSettings.AddSetting(src);

			SettingItemSpecial xwidth        = new SettingItemSpecial(flStr.FlashWidth,new StringDataType());
			xwidth.Order				     =	25; //2;
			xwidth.ExtendedGroupName         =   extgrp;
			bSettings.AddSetting(xwidth);

			SettingItemSpecial height       =   new SettingItemSpecial(flStr.FlashHeight,new StringDataType());
			height.ExtendedGroupName        =   extgrp;
			height.Order			        =	30; //3;
			bSettings.AddSetting(height);

			SettingItemSpecial backColor	=	new SettingItemSpecial(flStr.FlashBackColor,new ColorDataType ());
			backColor.Required		        = 	false;
			backColor.Value			        =	"#FFFFFF";
			backColor.ExtendedGroupName     =   extgrp;
			backColor.Order			        =	35; //4;
			bSettings.AddSetting(backColor);     

			SettingItemSpecial FlashPath	=	new SettingItemSpecial(flStr.FlashPath,new PortalUrlDataType());
			FlashPath.Required		        =	false;
			FlashPath.Value			        =	AddonNice.Settings.Path.WebPathCombine(AddonNice.Settings.Path.ApplicationRoot, portalSettings.PortalPath,"/FlashGallery/");
			FlashPath.ExtendedGroupName     =   extgrp;
			FlashPath.Order			        =	40; //5;
			bSettings.AddSetting(FlashPath);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FlashModuleSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }
    /// <summary>
    /// Include a Flash module in page
    /// </summary>
    public partial class FlashModule : PortalModuleControl 
	{
		public string FileSource;   


		#region General Implementation
		public override Guid GuidID 
		{
			get
			{
				return new Guid("{623EC4DD-BA40-421c-887D-D774ED8EBF02}");
			}
		}
		#endregion

		#region CTor and Initializer

        public FlashModule() 
		{
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]FlashModule Ctor Called.",ModuleTraceSwitch.Sw.Info);
		}

        public override string ExtendedResourcesGroup 
		{
            get
            {
                const string extgrp             =   "FLASHMODULE";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new FlashModuleSI(bSettings,this);
        }

		#endregion

        protected void Page_Load(object sender, System.EventArgs e) 
		{
			if (!IsPostBack)
			{
				string flashPath                =   Settings[flStr.FlashPath].FullPath;
				string flashSrc                 =   Settings[flStr.FlashSrc].Value;
				flashSrc	                    =	Path.WebPathCombine(flashPath,flashSrc);

				if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FlashModule Page_Load flashSrc: {0}", flashSrc),ModuleTraceSwitch.Sw.Info);
				flashSrc                        =   flashSrc.Replace("~~", portalSettings.PortalFullPath);
				flashSrc                        =   flashSrc.Replace("~", portalSettings.PortalPath);

				string flashHeight              =   Settings[flStr.FlashHeight].Value;
				string flashWidth               =   Settings[flStr.FlashWidth].Value;
				string flashBGColor             =   Settings[flStr.FlashBackColor].Value;

				//Set the output type and Movie
				this.FlashMovie1.FlashOutputType = FlashOutputType.FlashOnly; //Was ClientScriptVersionDection;

				//Always make sure you have a valid movie or your browser will hang.
				if (string.IsNullOrEmpty(flashSrc) || !System.IO.File.Exists(Server.MapPath(flashSrc)))
				{
					flashSrc = Path.WebPathCombine(AddonNice.Settings.Path.ApplicationRoot, portalSettings.PortalPath, "/FlashGallery/" + flashSrc);
					if (ModuleTraceSwitch.Sw.TraceVerbose)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]FlashModule Page_Load flashSrc 2: {0}", flashSrc),ModuleTraceSwitch.Sw.Info);
				}

				//Added existing file check by Manu
				if (System.IO.File.Exists(Server.MapPath(flashSrc)))
				{
					FlashMovie1.MovieName                       = flashSrc;

					// Set the plugin version to check for
					FlashMovie1.MajorPluginVersion              = 7;
					FlashMovie1.MajorPluginVersionRevision      = 0;
					FlashMovie1.MinorPluginVersion              = 0;
					FlashMovie1.MinorPluginVersionRevision      = 0;

					//Set some other properties
					if ( !string.IsNullOrEmpty(flashWidth) )
						FlashMovie1.MovieWidth = flashWidth;
					if ( !string.IsNullOrEmpty(flashHeight) )
						FlashMovie1.MovieHeight = flashHeight;

					if ( !string.IsNullOrEmpty(flashBGColor) && flashBGColor != "0")
					{
						try
						{
							FlashMovie1.MovieBGColor = System.Drawing.ColorTranslator.FromHtml(flashBGColor);
						}
						catch (Exception ex)
						{
							if (ModuleTraceSwitch.Sw.TraceError)
                                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]FlashModule Page_Load MovieBGColor ex: {0}", ex),ModuleTraceSwitch.Sw.Info);
						}
					}

					//this.FlashMovie1.AutoLoop = false;
					//this.FlashMovie1.AutoPlay = true;
					//this.FlashMovie1.FlashHorizontalAlignment = FlashHorizontalAlignment.Center;
					//this.FlashMovie1.FlashVerticalAlignment = FlashVerticalAlignment.Center;
					//this.FlashMovie1.HtmlAlignment = FlashHtmlAlignment.None;
					//this.FlashMovie1.UseDeviceFonts = false;
					//this.FlashMovie1.WindowMode = FlashMovieWindowMode.Transparent;
					//this.FlashMovie1.ShowMenu = false;
					//this.FlashMovie1.MovieQuality = FlashMovieQuality.AutoHigh;
					//this.FlashMovie1.MovieScale = FlashMovieScale.NoScale;

					// Add some variables
					//this.FlashMovie1.MovieVariables.Add("MyVar1","MyValue1");
					//this.FlashMovie1.MovieVariables.Add("MyVar2","MyValue2");
					//this.FlashMovie1.MovieVariables.Add("MyVar3","MyValue3");

					//Set the NoScript and NoFlash content.  
					//In most situations where
					//html will be displayed the content is the same for both

					//this.FlashMovie1.NoFlashContainer.Controls.Add("No flash found!");
					//this.FlashMovie1.NoScriptContainer.Controls.Add("Scripting is disabled");
				}
				else
				{
					FlashMovie1.Visible         = false;
					ErrorLabel.Text             = "File '" + flashSrc + "' not found!";
					ErrorLabel.Visible          = true;
				}
			}
		}

		#region Std Overrides

        public override void Page_PreInit(object sender, EventArgs e)
        {
			this.EditUrl = Config.ModulesPath+"/FlashModule/FlashEdit.aspx";
            base.Page_PreInit(sender, e);
        }
		override protected void OnInit(EventArgs e) 
		{
			InitializeComponent();
			base.OnInit(e);
		}
		private void InitializeComponent() 
		{
			Load		+=  new EventHandler(Page_Load);
		}
		#endregion
	}
}
